package com.ag.wsq.action;
/*
 * @author 王淑其
 */
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.write.DateFormat;
import jxl.write.DateTime;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import org.apache.struts2.ServletActionContext;

import com.ag.bean.TbSched;
import com.ag.common.BaseAction;
import com.ag.common.PageUtil;
import com.ag.wsq.service.impl.SchedServiceImpl;

import excel.FormatFactory;


@SuppressWarnings("serial")
public class ExportExcleAction extends BaseAction {

	private SchedServiceImpl schedServiceImpl;
	private PageUtil  pageUtil;
	private int page;
	private int maxRow;
	private int total;
	private List result;
	private String UId;
	private String UName;
	private String timeStart;
	private String timeEnd;
	private String name;
	
	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

	public String export() throws Exception {
		
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();

		// 从数据库中取得所要导入的数据
		
		pageUtil = schedServiceImpl.queryAll(total, page, maxRow, UId, UName, timeStart, timeEnd);
		
		List list = new ArrayList();
		String much = request.getParameter("much");		
		if(much.equals("本页")){
			list = pageUtil.getList();
		}else if(much.equals("全部")){
			list = schedServiceImpl.query(UId, UName, timeStart, timeEnd).getList();
		}else{
			list = null;
		}
		//设置资源头信息
		response.reset();
		response.setContentType("application/vnd,ms-excel");
		response.setHeader("Content-Disposition", "filename=excel.xls");
		OutputStream os = response.getOutputStream();
				
		//List list = new ArrayList();
		
		WritableWorkbook wb = Workbook.createWorkbook(os);
		WritableSheet sheet = wb.createSheet("sheet", 0);
		//WritableCellFormat wc = new WritableCellFormat();	
	
		// 分别设置单元格的高度＆宽度
		
		sheet.setColumnView(0, 20);
		sheet.setColumnView(1, 20);
		sheet.setColumnView(2, 20);
		sheet.setColumnView(3, 20);
		//sheet.setColumnView(4, 20);
	
		// 设置表中各列名称
		FormatFactory factory = new FormatFactory();
		//在FormatFactory中调出Excle的样式
		Label label = null;	
		label = new Label(0,0,"日期",factory.getFormat(1));
		sheet.addCell(label);
		label = new Label(1,0,"岗位",factory.getFormat(1));
		sheet.addCell(label);
		label = new Label(2,0,"班次",factory.getFormat(1));
		sheet.addCell(label);
		label = new Label(3,0,"值班人员",factory.getFormat(1));
		sheet.addCell(label);
		//label = new Label(4,0,"登录名",factory.getFormat(1));
		//sheet.addCell(label);
		
		// 设置各列值
		if(list==null){
			wb.write();
			wb.close();		
			os.close();	
			return null;
		}
		
		if(list.size()>0){
			int row = 1;			
			String content = null;
			for(row=1;row<=list.size();row++){
				TbSched tbSched = new TbSched();
				tbSched = (TbSched) list.get(row-1);
							
				// 设置时间
				if(tbSched.getId().getScTime()==null){
					label = new Label(0,row,"无",factory.getFormat(2));					
					sheet.addCell(label);
				}else {
					DateFormat df = new DateFormat("yyyy-MM-dd");					
					WritableCellFormat wcf = new WritableCellFormat(df);
					wcf.setAlignment(jxl.format.Alignment.CENTRE);
					DateTime labelDTF = new DateTime(0,row,tbSched.getId().getScTime(),wcf);	
					sheet.addCell(labelDTF);
				}												
				label = new Label(1,row,tbSched.getTbDuty().getDuId(),factory.getFormat(2));
				sheet.addCell(label);
				label = new Label(2,row,tbSched.getTbDetailShift().getDsName(),factory.getFormat(2));
				sheet.addCell(label);
				label = new Label(3,row,tbSched.getTbUser().getUName(),factory.getFormat(2));
				sheet.addCell(label);
				//label = new Label(4,row,tbSched.getTbUser().getUId(),factory.getFormat(2));
				//sheet.addCell(label);
			}
		}	
		// 结束
		wb.write();
		wb.close();		
		os.close();		
		return null;
	}

	public PageUtil getPageUtil() {
		return pageUtil;
	}

	public void setPageUtil(PageUtil pageUtil) {
		this.pageUtil = pageUtil;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getMaxRow() {
		return maxRow;
	}

	public void setMaxRow(int maxRow) {
		this.maxRow = maxRow;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public List getResult() {
		return result;
	}

	public void setResult(List result) {
		this.result = result;
	}

	public String getUId() {
		return UId;
	}

	public void setUId(String uId) {
		UId = uId;
	}

	public String getTimeStart() {
		return timeStart;
	}

	public void setTimeStart(String timeStart) {
		this.timeStart = timeStart;
	}

	public String getTimeEnd() {
		return timeEnd;
	}

	public void setTimeEnd(String timeEnd) {
		this.timeEnd = timeEnd;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public SchedServiceImpl getSchedServiceImpl() {
		return schedServiceImpl;
	}

	public void setSchedServiceImpl(SchedServiceImpl schedServiceImpl) {
		this.schedServiceImpl = schedServiceImpl;
	}

	public String getUName() {
		return UName;
	}

	public void setUName(String uName) {
		UName = uName;
	}


}
